//
//  STTools_ChildInterestionTableViewCell.m
//  FindFriend
//
//  Created by apple on 2020/9/3.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THTieZIItemView.h"

@interface THTieZIItemView()<STBWBrowserViewControllerDelegate>
@property(nonatomic, strong) UIView                     *whiteView;/**<  */
@property(nonatomic, strong) UIView                     *imageContentView;/**<  */

@property(nonatomic, strong) STButton                     *iconButton;/**<  */
@property(nonatomic, strong) STLabel                     *nickLable;/**<  */
@property(nonatomic, strong) STLabel                     *timeLable;/**<  */
@property(nonatomic, strong) YYLabel                     *contentLable;/**<  */

@property(nonatomic, strong) STButton                     *goodButton;/**<  */
@property(nonatomic, strong) CALayer                     *sublayer;/**<  */
@end
@implementation THTieZIItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self stoneobs_ui_configSubView];
    }
    return self;
}
- (void)setModel:(THTieZiModel *)model{
    _model = model;
    [self stoneobs_ui_configSubView];
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    [self removeAllSubviews];
    __weak typeof(self) weakSelf =  self;
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width - 0, self.height -0)];
    self.whiteView = whiteView;
    whiteView.layer.cornerRadius = 2;
    whiteView.backgroundColor = UIColor.whiteColor;
    whiteView.clipsToBounds = YES;
    whiteView.userInteractionEnabled = YES;
    [self addSubview:whiteView];
    self.whiteView = whiteView;
    
    ({
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(10, 15, 40, 40)
                                                                  title:nil
                                                             titleColor:nil
                                                              titleFont:0
                                                           cornerRadius:0
                                                        backgroundColor:nil
                                                        backgroundImage:STFDRandomImageProvider.random_livegirl_image
                                                                  image:nil];
        
        [stoneobs_ui_button sd_setImageWithURL:self.model.user_header_img.mj_url forState:UIControlStateNormal];
        [stoneobs_ui_button stbw_setBorderWith:2 borderColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR cornerRadius:20];
        [self.whiteView addSubview:stoneobs_ui_button];
        self.iconButton = stoneobs_ui_button;
        __weak typeof(self) weakSelf =  self;
        [stoneobs_ui_button setClicAction:^(UIButton *sender) {
            [TMUtils goto_user_info_witdh_id:weakSelf.model.user_id];
        }];
        
        
    });

    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(self.iconButton.right + 10, self.iconButton.top, 200, 20)
                                                                text:self.model.user_name
                                                           textColor:UIColor.blackColor
                                                                font:15
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [self.whiteView addSubview:stoneobs_ui_label];
        self.nickLable = stoneobs_ui_label;
        stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:15];
    });
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(self.iconButton.right + 10, self.nickLable.bottom +3, UISCREEN_WIDTH - 120, 20)
                                                                text:[NSString stringWithFormat:@"%@",self.model.created_at]
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:12
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [self.whiteView addSubview:stoneobs_ui_label];
        self.timeLable = stoneobs_ui_label;

    });
    
    if (self.model.cus_is_mine) {
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, 10, 40, 40)
                                                                  title:nil
                                                             titleColor:UIColor.whiteColor
                                                              titleFont:12
                                                           cornerRadius:20
                                                        backgroundColor:WH_UICOLOR_FROM_RGBA(0xEEEEEE)
                                                        backgroundImage:nil
                                                                  image:ST_IMAGE_NAMED(@"删除_white")];
        self.deleteButton = stoneobs_ui_button;
        stoneobs_ui_button.centerY = self.iconButton.centerY;
        stoneobs_ui_button.right = self.width - 15;
        [stoneobs_ui_button setClicAction:^(UIButton *sender) {
            
        }];
        stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self addSubview:stoneobs_ui_button];
    }else{
        ({
            STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 58, 24)
                                                                      title:@"+关注"
                                                                 titleColor:UIColor.whiteColor
                                                                  titleFont:12
                                                               cornerRadius:12
                                                            backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                                            backgroundImage:nil
                                                                      image:nil];
            self.attentionButton = stoneobs_ui_button;
            stoneobs_ui_button.centerY = self.iconButton.centerY;
            stoneobs_ui_button.right = self.width - 15;
            [stoneobs_ui_button setClicAction:^(UIButton *sender) {
                
                [THTreeHoleDataController addAttentionOrCancleFormUser_id:weakSelf.model.user_id handle:^(NSError *error, BOOL success, NSDictionary *resp) {
                   
                    sender.selected = !sender.selected;
                    weakSelf.model.is_follow =  !weakSelf.model.is_follow;
                    if (weakSelf.model.is_follow) {
                        sender.backgroundColor = WH_UICOLOR_FROM_RGBA(0xEEEEEE);
                    }else{
                        sender.backgroundColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
                    }
                    
                    //发送一个更新通知
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"update_list_notifi" object:weakSelf.model];
                }];
            }];
            [stoneobs_ui_button setTitleColor:WH_UICOLOR_FROM_RGBA(0x9A9A9A) forState:UIControlStateSelected];
            [stoneobs_ui_button setTitle:@"已关注" forState:UIControlStateSelected];
            stoneobs_ui_button.selected = self.model.is_follow;
            if (stoneobs_ui_button.selected) {
                stoneobs_ui_button.backgroundColor = WH_UICOLOR_FROM_RGBA(0xEEEEEE);
            }
            [whiteView addSubview:stoneobs_ui_button];
            stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        });
    }
    
    
    ({
        

            self.contentLable = YYLabel.new;
            self.contentLable.frame = CGRectMake(10, self.iconButton.bottom +10, self.whiteView.width-20, 61);
            self.contentLable.textColor = STONEOBS_UI_THIRD_TEXT_COLOR;
            self.contentLable.font = [UIFont systemFontOfSize:13];
            self.contentLable.textAlignment = NSTextAlignmentLeft;
            self.contentLable.numberOfLines = 0;
            self.contentLable.userInteractionEnabled =YES;
        
        [whiteView addSubview:self.contentLable];
        NSString * title = @"";
        if (self.model.content.length > 0) {
            title = self.model.content;
        }
        if (self.model.type.intValue == 2) {
            title = self.model.title;
        }
        NSAttributedString * rstr = [NSAttributedString.alloc initWithString:title];
        self.contentLable.textVerticalAlignment = YYTextVerticalAlignmentTop;
        self.contentLable.attributedText = rstr;
        
        CGSize size = [self.contentLable sizeThatFits:CGSizeMake(self.contentLable.width, CGFLOAT_MAX)];
        self.contentLable.truncationToken = nil;
        if (!self.model.cus_open) {
            if (size.height > self.contentLable.height) {
                [self addSeeMoreButton];
            }else{
                self.contentLable.height = size.height;
            }
        }else{
            self.contentLable.height = size.height;
        }
        
    });
    
    
    UIView * contentView = [[UIView alloc] initWithFrame:CGRectMake(10, self.contentLable.bottom + 10, self.whiteView.width - 20, 105)];
    [self.whiteView addSubview:contentView];
    self.imageContentView = contentView;
    if (self.model.imgs.count == 0) {
        contentView.height = 0;
    }
    whiteView.height = contentView.bottom + 10;
    self.height = whiteView.bottom;

    [self.imageContentView removeAllSubviews];
    CGFloat imageWitdh = (self.imageContentView.width - 2*10 - 5)/3;
    NSInteger min = MIN(self.model.imgs.count, 999999);
    for (NSInteger i = 0; i < min; i ++) {

        UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageWitdh, imageWitdh)];
        NSString * url = self.model.imgs[i];
        [stoneobs_ui_imageView sd_setImageWithURL:url.mj_url];
        [self.imageContentView addSubview:stoneobs_ui_imageView];
        stoneobs_ui_imageView.layer.cornerRadius = 4;
        stoneobs_ui_imageView.backgroundColor = WH_UICOLOR_FROM_RGBA(0xF6F6F6);
        stoneobs_ui_imageView.clipsToBounds = YES;
        stoneobs_ui_imageView.contentMode = UIViewContentModeScaleAspectFill;
        stoneobs_ui_imageView.userInteractionEnabled = YES;
        stoneobs_ui_imageView.left = imageWitdh * (i%3) + 10*(i%3);
        stoneobs_ui_imageView.top = stoneobs_ui_imageView.height * (i/3) + 10*(i/3);
        UITapGestureRecognizer * tap = [UITapGestureRecognizer.alloc initWithActionBlock:^(id  _Nonnull sender) {
            [weakSelf onSelctedButtonWithIndex:i];
        }];
        [stoneobs_ui_imageView addGestureRecognizer:tap];
        contentView.height = stoneobs_ui_imageView.bottom + 10;
        
        if (self.model.type.intValue == 2) {
            UIImageView * play_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
            play_imageView.image = [UIImage imageNamed:@"mi_pei_icon_play"];
            play_imageView.centerX = stoneobs_ui_imageView.width / 2;
            play_imageView.centerY = stoneobs_ui_imageView.height / 2;
            [stoneobs_ui_imageView addSubview:play_imageView];
            stoneobs_ui_imageView.userInteractionEnabled = NO;
        }
    }
    
    ({
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(15, contentView.bottom+10, 80, 40)
                                                                  title:@(self.model.share_num).description
                                                             titleColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                              titleFont:14
                                                           cornerRadius:0
                                                        backgroundColor:nil
                                                        backgroundImage:nil
                                                                  image:ST_IMAGE_NAMED(@"treehole_分享")];
        stoneobs_ui_button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        [whiteView addSubview:stoneobs_ui_button];
        [stoneobs_ui_button setClicAction:^(UIButton *sender) {
            [TMUtils goto_share_witdh_url:weakSelf.model.t_id];;
        }];
    });
    
    STButton * stoneobs_ui_good_button = [[STButton alloc] initWithFrame:CGRectMake(15, contentView.bottom+10, 65, 40)
                                                              title:@(self.model.up_num).description
                                                         titleColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                          titleFont:14
                                                       cornerRadius:0
                                                    backgroundColor:nil
                                                    backgroundImage:nil
                                                              image:ST_IMAGE_NAMED(@"treehole_点赞")];
    stoneobs_ui_good_button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
    [stoneobs_ui_good_button setTitleColor:FlatRed forState:UIControlStateSelected];
    [stoneobs_ui_good_button setImage:ST_IMAGE_NAMED(@"treehole_赞") forState:UIControlStateSelected];
    [whiteView addSubview:stoneobs_ui_good_button];
    stoneobs_ui_good_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [stoneobs_ui_good_button setClicAction:^(UIButton *sender) {
        
        [THTreeHoleDataController addGoodOrCancleFormPosts_id:weakSelf.model.t_id handle:^(NSError *error, BOOL success, NSDictionary *resp) {
           
            sender.selected = !sender.selected;
            weakSelf.model.is_up =  !weakSelf.model.is_up;
            if (weakSelf.model.is_up) {
                weakSelf.model.up_num = weakSelf.model.up_num+1;
            }else{
                weakSelf.model.up_num = weakSelf.model.up_num-1;
            }
            [sender setTitle:@(weakSelf.model.up_num).description forState:UIControlStateNormal];
        }];
    }];
    stoneobs_ui_good_button.selected = self.model.is_up;
    stoneobs_ui_good_button.right = UISCREEN_WIDTH - 15;
    
    STButton * stoneobs_ui_comment_button = [[STButton alloc] initWithFrame:CGRectMake(15, contentView.bottom+10, 65, 40)
                                                              title:@(self.model.comment_num).description
                                                         titleColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                          titleFont:14
                                                       cornerRadius:0
                                                    backgroundColor:nil
                                                    backgroundImage:nil
                                                              image:ST_IMAGE_NAMED(@"treehole_消息")];
    stoneobs_ui_comment_button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
    stoneobs_ui_comment_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [whiteView addSubview:stoneobs_ui_comment_button];
    [stoneobs_ui_comment_button setClicAction:^(UIButton *sender) {
        
        THAddcommentViewController * vc = THAddcommentViewController.new;
        [vc setDidFillComment:^(NSString * _Nonnull comment) {
            [THTreeHoleDataController addcommentFormPosts_id:weakSelf.model.t_id content:comment handle:^(NSError *error, BOOL success, NSDictionary *resp) {
               
                if (success) {
                    weakSelf.model.comment_num = weakSelf.model.comment_num+1;
                    weakSelf.model = weakSelf.model;
                    [SVProgressHUD showSuccessWithStatus:@"评论成功"];
                }
            }];
        }];
        [TMUtils.stoneobs_fun_get_currentTopController pushViewController:vc animated:1];
        
    }];
    stoneobs_ui_comment_button.right = stoneobs_ui_good_button.left-5;
//    stoneobs_ui_comment_button.userInteractionEnabled = NO;
    whiteView.height = stoneobs_ui_comment_button.bottom + 10;
    
    UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(0, whiteView.height, UISCREEN_WIDTH, 10)];
    stoneobs_ui_line.backgroundColor = WH_UICOLOR_FROM_RGBA(0xEEEEEE);
    [self addSubview:stoneobs_ui_line];
    
    self.height = stoneobs_ui_line.bottom;
    self.model.cus_height = self.height;
}
- (void)onSelctedButtonWithIndex:(NSInteger)index{
    //点击了某个图片
    NSMutableArray * models = [NSMutableArray new];
    NSArray * modeArray = self.imageContentView.subviews;
    for (UIImageView * url in modeArray) {
        STBWPhotoModel * model = [STBWPhotoModel  new];
        model.originImage = url.image;
        [models addObject:model];
    }
    STBWBrowserViewController * vc = [[STBWBrowserViewController alloc] initWithArray:models curentIndex:index];
    vc.shouldHideBottomView = YES;
    vc.delegate = self;
    vc.shouldHideTopView = YES;
    UIViewController * rootVC = TMUtils.stoneobs_fun_get_currentTopController;
    [rootVC presentViewController:vc animated:NO completion:nil];
}

#pragma mark --STBWBrowserViewControllerDelegate
- (UIView*)browserControllerDidScrollToIndexpath:(NSIndexPath *)indexPath model:(STBWPhotoModel *)model controller:(STBWBrowserViewController *)controller{

    return   self.imageContentView.subviews[indexPath.row];
}
- (void)rightBarActionFromController:(STBWBrowserViewController *)controller currentIndexPath:(NSIndexPath *)curentIndexpath{


}
- (void)layoutSubviews{
    [self.sublayer removeFromSuperlayer];
//    self.sublayer =  [self.whiteView stoneobs_fun_st_showRoundLayer];
}

- (void)addSeeMoreButton {
    
    __weak typeof(self) weakSelf =  self;
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"...展开"];
    YYTextHighlight *hi = [YYTextHighlight new];
    [hi setColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR];
    hi.tapAction = ^(UIView *containerView,NSAttributedString *text,NSRange range, CGRect rect) {
        if (weakSelf.onselctedOpenButton) {
            weakSelf.onselctedOpenButton(weakSelf.model);
        }
    };
    text.font = weakSelf.contentLable.font;
    [text setFont:[UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:13] range:[text.string rangeOfString:@"展开"]];
    [text setColor:WH_UICOLOR_FROM_RGBA(0x35A6E6) range:[text.string rangeOfString:@"展开"]];
    [text setTextHighlight:hi range:[text.string rangeOfString:@"展开"]];
    YYLabel *seeMore = [YYLabel new];
    seeMore.attributedText = text;
    [seeMore sizeToFit];
    NSAttributedString *truncationToken = [NSAttributedString attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.frame.size alignToFont:text.font alignment:YYTextVerticalAlignmentCenter];
    self.contentLable.truncationToken = truncationToken;
    
}

@end
